package com.chujian.Adapter;

import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Paint;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.chujian.Activities.OrderCurrentDetailSellerActivity;
import com.chujian.Threads.HttpClientPostThread;
import com.chujian.Utils.Constan;
import com.chujian.Utils.Util;
import com.chujian.model.FoodList;
import com.chujian.orderingsystem.R;
import com.google.gson.Gson;

public class OrderCurrentDetailSellerAdapter extends BaseAdapter {
	private LayoutInflater mLayoutInflater;
	private List<FoodList> mList;
	private Context context;
	private int modifyPosition=-1;
	private Handler mHandler=new Handler(){
		public void handleMessage(android.os.Message msg) {
			super.handleMessage(msg);
			String str=(String) msg.obj;
			if(str.equals("foodBackSuccess")){
				Util.showTextShort(context, "退菜成功");
				mList.get(modifyPosition).setFoodStatus(2);
				notifyDataSetChanged();
			}else if(str.equals("foodBackFailed")){
				Util.showTextShort(context, "退菜失败");
			}
		};
	};
	
	public OrderCurrentDetailSellerAdapter(List<FoodList> mList, Context context) {
		super();
		this.mList = mList;
		this.context = context;
		mLayoutInflater=LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final int pos=position;
		// TODO Auto-generated method stub
		ViewHolder holder=null;
		if(convertView==null){
			holder=new ViewHolder();
			convertView=mLayoutInflater.inflate(R.layout.order_current_detail_seller_item, null);
			holder.tvFoodName=(TextView) convertView.findViewById(R.id.tv_order_current_dt_seller_foodname);
			holder.tvFoodCount=(TextView) convertView.findViewById(R.id.tv_order_current_dt_seller_foodcount);
			holder.btnStart=(Button) convertView.findViewById(R.id.btn_order_current_dt_seller_start);
			holder.btnComplete=(Button) convertView.findViewById(R.id.btn_order_current_dt_seller_complete);
			convertView.setTag(holder);
		}else {
			holder=(ViewHolder) convertView.getTag();
		}
		holder.btnStart.setVisibility(100);
		holder.btnComplete.setVisibility(100);
		
		if(mList.get(position).getFoodStatus()==1){
			holder.btnStart.setVisibility(0);
			holder.btnComplete.setVisibility(0);
		}else if(mList.get(position).getFoodStatus()==2){

		holder.tvFoodName.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);

		}
		holder.btnStart.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
//				String url=Constan.BASE_URL+"";
				Util.showTextShort(context, "退菜");
				confirmDialog(pos);
			}
		});
		holder.btnComplete.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Util.showTextShort(context, "拒绝");
			}
		});
		
		holder.tvFoodName.setText(mList.get(position).getFoodName());
		holder.tvFoodCount.setText(mList.get(position).getFoodAmount()+"份");
		
		return convertView;
	}
	class ViewHolder{
		public TextView tvFoodName;
		public TextView tvFoodCount;
		public Button btnStart;
		public Button btnComplete;
	}
	private void confirmDialog(final int position) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle("警告！");
        builder.setMessage("确认退菜吗？");
        builder.setPositiveButton("确认", new DialogInterface.OnClickListener(){
            @Override
            public void onClick(DialogInterface dialog, int which){
            	modifyPosition=position;
            	Util.showTextLong(context, "正在进行退菜操作，请稍候....");
				String url = Constan.BASE_URL + "FoodBackSellerServlet";
				Gson gson = new Gson();
				String jsonString = gson.toJson(mList.get(position));
				new HttpClientPostThread(url, mHandler, jsonString).start();
				;
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener(){
            @Override
            public void onClick(DialogInterface dialog, int which){
            	
            }
        });
        builder.show();
	}
}
